{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import plotly\n",
    "import plotly.graph_objs as go\n",
    "import math\n",
    "import numpy as np\n",
    "plotly.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Colored and Styled -- Grouped(Stacked) Bar Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "marker": {
          "color": "rgb(158,202,225)",
          "line": {
           "color": "rgb(8,48,107)",
           "width": 1.5
          },
          "opacity": 0.6
         },
         "name": "SF Zoo",
         "text": [
          "gi: 20",
          "or: 14",
          "mo: 23",
          "ti: 5",
          "ra: 16"
         ],
         "textposition": "auto",
         "type": "bar",
         "uid": "8325a0bf-f6b1-4337-907a-96856cd48394",
         "x": [
          "giraffes",
          "orangutans",
          "monkeys",
          "tigers",
          "rabbits"
         ],
         "y": [
          20,
          14,
          23,
          5,
          16
         ]
        },
        {
         "name": "LA Zoo",
         "type": "bar",
         "uid": "d86180ce-9b1b-4206-9edc-540c8304f6c2",
         "x": [
          "giraffes",
          "orangutans",
          "monkeys",
          "tigers",
          "rabbits"
         ],
         "y": [
          12,
          18,
          29,
          8,
          21
         ]
        }
       ],
       "layout": {
        "bargap": 0.15,
        "bargroupgap": 0.05,
        "barmode": "group",
        "legend": {
         "bgcolor": "rgba(0, 0, 255, 0)",
         "bordercolor": "rgba(0, 0, 255, 0)",
         "x": 0,
         "y": 1
        },
        "title": "SF and LA Zoo Bar",
        "xaxis": {
         "tickangle": -45,
         "tickfont": {
          "color": "rgb(107, 107, 107)",
          "size": 14
         }
        },
        "yaxis": {
         "tickfont": {
          "color": "rgb(107, 107, 107)",
          "size": 14
         },
         "title": "number",
         "titlefont": {
          "color": "rgb(107, 107, 107)",
          "size": 16
         }
        }
       }
      },
      "text/html": [
       "<div id=\"ec74453e-ebc7-4beb-8eba-da7f4a959ab1\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"ec74453e-ebc7-4beb-8eba-da7f4a959ab1\", [{\"marker\": {\"color\": \"rgb(158,202,225)\", \"line\": {\"color\": \"rgb(8,48,107)\", \"width\": 1.5}, \"opacity\": 0.6}, \"name\": \"SF Zoo\", \"text\": [\"gi: 20\", \"or: 14\", \"mo: 23\", \"ti: 5\", \"ra: 16\"], \"textposition\": \"auto\", \"x\": [\"giraffes\", \"orangutans\", \"monkeys\", \"tigers\", \"rabbits\"], \"y\": [20, 14, 23, 5, 16], \"type\": \"bar\", \"uid\": \"8325a0bf-f6b1-4337-907a-96856cd48394\"}, {\"name\": \"LA Zoo\", \"x\": [\"giraffes\", \"orangutans\", \"monkeys\", \"tigers\", \"rabbits\"], \"y\": [12, 18, 29, 8, 21], \"type\": \"bar\", \"uid\": \"d86180ce-9b1b-4206-9edc-540c8304f6c2\"}], {\"bargap\": 0.15, \"bargroupgap\": 0.05, \"barmode\": \"group\", \"legend\": {\"bgcolor\": \"rgba(0, 0, 255, 0)\", \"bordercolor\": \"rgba(0, 0, 255, 0)\", \"x\": 0, \"y\": 1}, \"title\": \"SF and LA Zoo Bar\", \"xaxis\": {\"tickangle\": -45, \"tickfont\": {\"color\": \"rgb(107, 107, 107)\", \"size\": 14}}, \"yaxis\": {\"tickfont\": {\"color\": \"rgb(107, 107, 107)\", \"size\": 14}, \"title\": \"number\", \"titlefont\": {\"color\": \"rgb(107, 107, 107)\", \"size\": 16}}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"ec74453e-ebc7-4beb-8eba-da7f4a959ab1\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"ec74453e-ebc7-4beb-8eba-da7f4a959ab1\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"ec74453e-ebc7-4beb-8eba-da7f4a959ab1\", [{\"marker\": {\"color\": \"rgb(158,202,225)\", \"line\": {\"color\": \"rgb(8,48,107)\", \"width\": 1.5}, \"opacity\": 0.6}, \"name\": \"SF Zoo\", \"text\": [\"gi: 20\", \"or: 14\", \"mo: 23\", \"ti: 5\", \"ra: 16\"], \"textposition\": \"auto\", \"x\": [\"giraffes\", \"orangutans\", \"monkeys\", \"tigers\", \"rabbits\"], \"y\": [20, 14, 23, 5, 16], \"type\": \"bar\", \"uid\": \"8325a0bf-f6b1-4337-907a-96856cd48394\"}, {\"name\": \"LA Zoo\", \"x\": [\"giraffes\", \"orangutans\", \"monkeys\", \"tigers\", \"rabbits\"], \"y\": [12, 18, 29, 8, 21], \"type\": \"bar\", \"uid\": \"d86180ce-9b1b-4206-9edc-540c8304f6c2\"}], {\"bargap\": 0.15, \"bargroupgap\": 0.05, \"barmode\": \"group\", \"legend\": {\"bgcolor\": \"rgba(0, 0, 255, 0)\", \"bordercolor\": \"rgba(0, 0, 255, 0)\", \"x\": 0, \"y\": 1}, \"title\": \"SF and LA Zoo Bar\", \"xaxis\": {\"tickangle\": -45, \"tickfont\": {\"color\": \"rgb(107, 107, 107)\", \"size\": 14}}, \"yaxis\": {\"tickfont\": {\"color\": \"rgb(107, 107, 107)\", \"size\": 14}, \"title\": \"number\", \"titlefont\": {\"color\": \"rgb(107, 107, 107)\", \"size\": 16}}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"ec74453e-ebc7-4beb-8eba-da7f4a959ab1\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace0 = go.Bar(\n",
    "    x = ['giraffes', 'orangutans', 'monkeys', 'tigers', 'rabbits'],\n",
    "    y = [20, 14, 23, 5, 16],\n",
    "    name = 'SF Zoo',    \n",
    "    # 显示为横向的Bar，但此时要改变x、y的顺序\n",
    "    # orientation = 'h',\n",
    "    # 直接在图上显示text的内容\n",
    "    textposition = 'auto',\n",
    "    # Text的内容Hover Text\n",
    "    text = [\"gi: 20\", \"or: 14\", \"mo: 23\", \"ti: 5\", \"ra: 16\"],\n",
    "    marker = dict(\n",
    "        opacity=0.6,\n",
    "        color='rgb(158,202,225)',\n",
    "        line=dict(color='rgb(8,48,107)', width=1.5),\n",
    "    )\n",
    ")\n",
    "trace1 = go.Bar(\n",
    "    x = ['giraffes', 'orangutans', 'monkeys', 'tigers', 'rabbits'],\n",
    "    y = [12, 18, 29, 8, 21],\n",
    "    name = 'LA Zoo',\n",
    "    # 显示为横向的Bar，但此时要改变x、y的顺序\n",
    "    # orientation = 'h',\n",
    ")\n",
    "\n",
    "data = [trace0, trace1]\n",
    "layout = go.Layout(\n",
    "    title='SF and LA Zoo Bar',\n",
    "    xaxis=dict(\n",
    "        # 旋转X坐标角度\n",
    "        tickangle=-45,\n",
    "        tickfont=dict(size=14, color='rgb(107, 107, 107)')\n",
    "    ),\n",
    "    yaxis=dict(\n",
    "        title='number',\n",
    "        titlefont=dict(size=16, color='rgb(107, 107, 107)'),\n",
    "        tickfont=dict(size=14, color='rgb(107, 107, 107)')\n",
    "    ),\n",
    "    legend=dict(\n",
    "        # X/Y值标定图例的位置，范围从0到1\n",
    "        x=0,\n",
    "        y=1,\n",
    "        bgcolor='rgba(0, 0, 255, 0)',\n",
    "        bordercolor='rgba(0, 0, 255, 0)'\n",
    "    ),\n",
    "    # stack就是上下罗列\n",
    "    barmode='group',\n",
    "    bargap=0.15,\n",
    "    bargroupgap=0.05\n",
    ")\n",
    "fig = go.Figure(data=data, layout=layout)\n",
    "plotly.offline.iplot(fig, filename='grouped-bar')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Customizing Individual Bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "marker": {
          "color": [
           "rgba(204,204,204,1)",
           "rgba(222,45,38,0.8)",
           "rgba(204,204,204,1)",
           "rgba(204,204,204,1)",
           "rgba(204,204,204,1)"
          ]
         },
         "type": "bar",
         "uid": "0b236864-accf-4ef0-82b4-172c8938900a",
         "width": [
          0.8,
          0.5,
          0.8,
          1,
          0.5
         ],
         "x": [
          "Feature A",
          "Feature B",
          "Feature C",
          "Feature D",
          "Feature E"
         ],
         "y": [
          20,
          14,
          23,
          25,
          22
         ]
        }
       ],
       "layout": {
        "title": "Least Used Feature"
       }
      },
      "text/html": [
       "<div id=\"30cf507c-bf7e-450d-b2fb-879473041c62\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"30cf507c-bf7e-450d-b2fb-879473041c62\", [{\"marker\": {\"color\": [\"rgba(204,204,204,1)\", \"rgba(222,45,38,0.8)\", \"rgba(204,204,204,1)\", \"rgba(204,204,204,1)\", \"rgba(204,204,204,1)\"]}, \"width\": [0.8, 0.5, 0.8, 1, 0.5], \"x\": [\"Feature A\", \"Feature B\", \"Feature C\", \"Feature D\", \"Feature E\"], \"y\": [20, 14, 23, 25, 22], \"type\": \"bar\", \"uid\": \"0b236864-accf-4ef0-82b4-172c8938900a\"}], {\"title\": \"Least Used Feature\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"30cf507c-bf7e-450d-b2fb-879473041c62\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"30cf507c-bf7e-450d-b2fb-879473041c62\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"30cf507c-bf7e-450d-b2fb-879473041c62\", [{\"marker\": {\"color\": [\"rgba(204,204,204,1)\", \"rgba(222,45,38,0.8)\", \"rgba(204,204,204,1)\", \"rgba(204,204,204,1)\", \"rgba(204,204,204,1)\"]}, \"width\": [0.8, 0.5, 0.8, 1, 0.5], \"x\": [\"Feature A\", \"Feature B\", \"Feature C\", \"Feature D\", \"Feature E\"], \"y\": [20, 14, 23, 25, 22], \"type\": \"bar\", \"uid\": \"0b236864-accf-4ef0-82b4-172c8938900a\"}], {\"title\": \"Least Used Feature\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"30cf507c-bf7e-450d-b2fb-879473041c62\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace = go.Bar(\n",
    "    x=['Feature A', 'Feature B', 'Feature C', 'Feature D', 'Feature E'],\n",
    "    y=[20, 14, 23, 25, 22],\n",
    "    # 自定义宽度\n",
    "    width = [0.8, 0.5, 0.8, 1, 0.5],\n",
    "    # 自定义颜色\n",
    "    marker=dict(color=['rgba(204,204,204,1)', 'rgba(222,45,38,0.8)', 'rgba(204,204,204,1)', 'rgba(204,204,204,1)', 'rgba(204,204,204,1)']),\n",
    ")\n",
    "layout = go.Layout(title='Least Used Feature')\n",
    "fig = go.Figure(data=[trace], layout=layout)\n",
    "plotly.offline.iplot(fig, filename='custom-bar')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Customizing Individual Bar Base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "base": [
          -500,
          -600,
          -700
         ],
         "marker": {
          "color": "red"
         },
         "name": "expenses",
         "type": "bar",
         "uid": "3c1de37f-114d-4db4-8018-439a819e7235",
         "x": [
          "2016",
          "2017",
          "2018"
         ],
         "y": [
          500,
          600,
          700
         ]
        },
        {
         "base": 0,
         "marker": {
          "color": "blue"
         },
         "name": "revenue",
         "type": "bar",
         "uid": "22b3d3bf-ef78-4b18-ad07-b4a1a835345e",
         "x": [
          "2016",
          "2017",
          "2018"
         ],
         "y": [
          300,
          400,
          700
         ]
        }
       ],
       "layout": {}
      },
      "text/html": [
       "<div id=\"d4e41f48-ba0b-4581-98eb-599fc9c3ddd1\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"d4e41f48-ba0b-4581-98eb-599fc9c3ddd1\", [{\"base\": [-500, -600, -700], \"marker\": {\"color\": \"red\"}, \"name\": \"expenses\", \"x\": [\"2016\", \"2017\", \"2018\"], \"y\": [500, 600, 700], \"type\": \"bar\", \"uid\": \"bd5e8a02-dd17-48dd-96b1-d75dd0c76010\"}, {\"base\": 0, \"marker\": {\"color\": \"blue\"}, \"name\": \"revenue\", \"x\": [\"2016\", \"2017\", \"2018\"], \"y\": [300, 400, 700], \"type\": \"bar\", \"uid\": \"369376ad-e9c3-4f8b-93d5-8e10a52f9ffd\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"d4e41f48-ba0b-4581-98eb-599fc9c3ddd1\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"d4e41f48-ba0b-4581-98eb-599fc9c3ddd1\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"d4e41f48-ba0b-4581-98eb-599fc9c3ddd1\", [{\"base\": [-500, -600, -700], \"marker\": {\"color\": \"red\"}, \"name\": \"expenses\", \"x\": [\"2016\", \"2017\", \"2018\"], \"y\": [500, 600, 700], \"type\": \"bar\", \"uid\": \"bd5e8a02-dd17-48dd-96b1-d75dd0c76010\"}, {\"base\": 0, \"marker\": {\"color\": \"blue\"}, \"name\": \"revenue\", \"x\": [\"2016\", \"2017\", \"2018\"], \"y\": [300, 400, 700], \"type\": \"bar\", \"uid\": \"369376ad-e9c3-4f8b-93d5-8e10a52f9ffd\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"d4e41f48-ba0b-4581-98eb-599fc9c3ddd1\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace0 = go.Bar(\n",
    "    x = ['2016', '2017', '2018'],\n",
    "    y = [500, 600, 700],\n",
    "    base = [-500, -600, -700],\n",
    "    marker = dict(color = 'red'),\n",
    "    name = 'expenses'\n",
    ")\n",
    "trace1 = go.Bar(\n",
    "    x = ['2016', '2017', '2018'],\n",
    "    y = [300, 400, 700],\n",
    "    base = 0,\n",
    "    marker = dict(color = 'blue'),\n",
    "    name = 'revenue'\n",
    ")\n",
    "plotly.offline.iplot([trace0, trace1], filename='base-bar')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Waterfall Bar Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "marker": {
          "color": "rgba(1,1,1, 0.0)"
         },
         "type": "bar",
         "uid": "666be593-da4c-4ed6-aa70-d377a8d0a7e0",
         "x": [
          "Product<br>Revenue",
          "Services<br>Revenue",
          "Total<br>Revenue",
          "Fixed<br>Costs",
          "Variable<br>Costs",
          "Total<br>Costs",
          "Total"
         ],
         "y": [
          0,
          430,
          0,
          570,
          370,
          370,
          0
         ]
        },
        {
         "marker": {
          "color": "rgba(55, 128, 191, 0.7)",
          "line": {
           "color": "rgba(55, 128, 191, 1.0)",
           "width": 2
          }
         },
         "type": "bar",
         "uid": "5830cb51-a3cd-491e-9ebd-b60a7425c179",
         "x": [
          "Product<br>Revenue",
          "Services<br>Revenue",
          "Total<br>Revenue",
          "Fixed<br>Costs",
          "Variable<br>Costs",
          "Total<br>Costs",
          "Total"
         ],
         "y": [
          430,
          260,
          690,
          0,
          0,
          0,
          0
         ]
        },
        {
         "marker": {
          "color": "rgba(219, 64, 82, 0.7)",
          "line": {
           "color": "rgba(219, 64, 82, 1.0)",
           "width": 2
          }
         },
         "type": "bar",
         "uid": "87374e7c-da1d-4fe8-950e-d85e86878dd0",
         "x": [
          "Product<br>Revenue",
          "Services<br>Revenue",
          "Total<br>Revenue",
          "Fixed<br>Costs",
          "Variable<br>Costs",
          "Total<br>Costs",
          "Total"
         ],
         "y": [
          0,
          0,
          0,
          120,
          200,
          320,
          0
         ]
        },
        {
         "marker": {
          "color": "rgba(50, 171, 96, 0.7)",
          "line": {
           "color": "rgba(50, 171, 96, 1.0)",
           "width": 2
          }
         },
         "type": "bar",
         "uid": "f551ab13-2326-4be3-9620-b27c507dc9ff",
         "x": [
          "Product<br>Revenue",
          "Services<br>Revenue",
          "Total<br>Revenue",
          "Fixed<br>Costs",
          "Variable<br>Costs",
          "Total<br>Costs",
          "Total"
         ],
         "y": [
          0,
          0,
          0,
          0,
          0,
          0,
          370
         ]
        }
       ],
       "layout": {
        "annotations": [
         {
          "font": {
           "color": "rgba(245, 246, 249, 1)",
           "family": "Arial",
           "size": 14
          },
          "showarrow": false,
          "text": "$430K",
          "x": "Product<br>Revenue",
          "y": 400
         },
         {
          "font": {
           "color": "rgba(245, 246, 249, 1)",
           "family": "Arial",
           "size": 14
          },
          "showarrow": false,
          "text": "$260K",
          "x": "Services<br>Revenue",
          "y": 660
         },
         {
          "font": {
           "color": "rgba(245, 246, 249, 1)",
           "family": "Arial",
           "size": 14
          },
          "showarrow": false,
          "text": "$690K",
          "x": "Total<br>Revenue",
          "y": 660
         },
         {
          "font": {
           "color": "rgba(245, 246, 249, 1)",
           "family": "Arial",
           "size": 14
          },
          "showarrow": false,
          "text": "$-120K",
          "x": "Fixed<br>Costs",
          "y": 590
         },
         {
          "font": {
           "color": "rgba(245, 246, 249, 1)",
           "family": "Arial",
           "size": 14
          },
          "showarrow": false,
          "text": "$-200K",
          "x": "Variable<br>Costs",
          "y": 400
         },
         {
          "font": {
           "color": "rgba(245, 246, 249, 1)",
           "family": "Arial",
           "size": 14
          },
          "showarrow": false,
          "text": "$-320K",
          "x": "Total<br>Costs",
          "y": 400
         },
         {
          "font": {
           "color": "rgba(245, 246, 249, 1)",
           "family": "Arial",
           "size": 14
          },
          "showarrow": false,
          "text": "$370K",
          "x": "Total",
          "y": 340
         }
        ],
        "barmode": "stack",
        "paper_bgcolor": "rgba(245, 246, 249, 1)",
        "plot_bgcolor": "rgba(245, 246, 249, 1)",
        "showlegend": false,
        "title": "Annual Profit- 2015"
       }
      },
      "text/html": [
       "<div id=\"5bd31b3a-8a2e-4ddc-9962-b152b575cf51\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"5bd31b3a-8a2e-4ddc-9962-b152b575cf51\", [{\"marker\": {\"color\": \"rgba(1,1,1, 0.0)\"}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [0, 430, 0, 570, 370, 370, 0], \"type\": \"bar\", \"uid\": \"666be593-da4c-4ed6-aa70-d377a8d0a7e0\"}, {\"marker\": {\"color\": \"rgba(55, 128, 191, 0.7)\", \"line\": {\"color\": \"rgba(55, 128, 191, 1.0)\", \"width\": 2}}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [430, 260, 690, 0, 0, 0, 0], \"type\": \"bar\", \"uid\": \"5830cb51-a3cd-491e-9ebd-b60a7425c179\"}, {\"marker\": {\"color\": \"rgba(219, 64, 82, 0.7)\", \"line\": {\"color\": \"rgba(219, 64, 82, 1.0)\", \"width\": 2}}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [0, 0, 0, 120, 200, 320, 0], \"type\": \"bar\", \"uid\": \"87374e7c-da1d-4fe8-950e-d85e86878dd0\"}, {\"marker\": {\"color\": \"rgba(50, 171, 96, 0.7)\", \"line\": {\"color\": \"rgba(50, 171, 96, 1.0)\", \"width\": 2}}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [0, 0, 0, 0, 0, 0, 370], \"type\": \"bar\", \"uid\": \"f551ab13-2326-4be3-9620-b27c507dc9ff\"}], {\"annotations\": [{\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$430K\", \"x\": \"Product<br>Revenue\", \"y\": 400}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$260K\", \"x\": \"Services<br>Revenue\", \"y\": 660}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$690K\", \"x\": \"Total<br>Revenue\", \"y\": 660}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$-120K\", \"x\": \"Fixed<br>Costs\", \"y\": 590}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$-200K\", \"x\": \"Variable<br>Costs\", \"y\": 400}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$-320K\", \"x\": \"Total<br>Costs\", \"y\": 400}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$370K\", \"x\": \"Total\", \"y\": 340}], \"barmode\": \"stack\", \"paper_bgcolor\": \"rgba(245, 246, 249, 1)\", \"plot_bgcolor\": \"rgba(245, 246, 249, 1)\", \"showlegend\": false, \"title\": \"Annual Profit- 2015\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"5bd31b3a-8a2e-4ddc-9962-b152b575cf51\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"5bd31b3a-8a2e-4ddc-9962-b152b575cf51\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"5bd31b3a-8a2e-4ddc-9962-b152b575cf51\", [{\"marker\": {\"color\": \"rgba(1,1,1, 0.0)\"}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [0, 430, 0, 570, 370, 370, 0], \"type\": \"bar\", \"uid\": \"666be593-da4c-4ed6-aa70-d377a8d0a7e0\"}, {\"marker\": {\"color\": \"rgba(55, 128, 191, 0.7)\", \"line\": {\"color\": \"rgba(55, 128, 191, 1.0)\", \"width\": 2}}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [430, 260, 690, 0, 0, 0, 0], \"type\": \"bar\", \"uid\": \"5830cb51-a3cd-491e-9ebd-b60a7425c179\"}, {\"marker\": {\"color\": \"rgba(219, 64, 82, 0.7)\", \"line\": {\"color\": \"rgba(219, 64, 82, 1.0)\", \"width\": 2}}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [0, 0, 0, 120, 200, 320, 0], \"type\": \"bar\", \"uid\": \"87374e7c-da1d-4fe8-950e-d85e86878dd0\"}, {\"marker\": {\"color\": \"rgba(50, 171, 96, 0.7)\", \"line\": {\"color\": \"rgba(50, 171, 96, 1.0)\", \"width\": 2}}, \"x\": [\"Product<br>Revenue\", \"Services<br>Revenue\", \"Total<br>Revenue\", \"Fixed<br>Costs\", \"Variable<br>Costs\", \"Total<br>Costs\", \"Total\"], \"y\": [0, 0, 0, 0, 0, 0, 370], \"type\": \"bar\", \"uid\": \"f551ab13-2326-4be3-9620-b27c507dc9ff\"}], {\"annotations\": [{\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$430K\", \"x\": \"Product<br>Revenue\", \"y\": 400}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$260K\", \"x\": \"Services<br>Revenue\", \"y\": 660}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$690K\", \"x\": \"Total<br>Revenue\", \"y\": 660}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$-120K\", \"x\": \"Fixed<br>Costs\", \"y\": 590}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$-200K\", \"x\": \"Variable<br>Costs\", \"y\": 400}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$-320K\", \"x\": \"Total<br>Costs\", \"y\": 400}, {\"font\": {\"color\": \"rgba(245, 246, 249, 1)\", \"family\": \"Arial\", \"size\": 14}, \"showarrow\": false, \"text\": \"$370K\", \"x\": \"Total\", \"y\": 340}], \"barmode\": \"stack\", \"paper_bgcolor\": \"rgba(245, 246, 249, 1)\", \"plot_bgcolor\": \"rgba(245, 246, 249, 1)\", \"showlegend\": false, \"title\": \"Annual Profit- 2015\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"5bd31b3a-8a2e-4ddc-9962-b152b575cf51\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_data = ['Product<br>Revenue', 'Services<br>Revenue', 'Total<br>Revenue', \n",
    "          'Fixed<br>Costs', 'Variable<br>Costs', 'Total<br>Costs', 'Total']\n",
    "y_data = [400, 660, 660, 590, 400, 400, 340]\n",
    "text = ['$430K', '$260K', '$690K', '$-120K', '$-200K', '$-320K', '$370K']\n",
    "\n",
    "# Base\n",
    "trace0 = go.Bar(\n",
    "    x=x_data, y=[0, 430, 0, 570, 370, 370, 0],\n",
    "    marker=dict(color='rgba(1,1,1, 0.0)')\n",
    ")\n",
    "# Revenue\n",
    "trace1 = go.Bar(\n",
    "    x=x_data, y=[430, 260, 690, 0, 0, 0, 0],\n",
    "    marker=dict(\n",
    "        color='rgba(55, 128, 191, 0.7)',\n",
    "        line=dict(color='rgba(55, 128, 191, 1.0)', width=2)\n",
    "    )\n",
    ")\n",
    "# Costs\n",
    "trace2 = go.Bar(\n",
    "    x=x_data, y=[0, 0, 0, 120, 200, 320, 0],\n",
    "    marker=dict(\n",
    "        color='rgba(219, 64, 82, 0.7)', \n",
    "        line=dict(color='rgba(219, 64, 82, 1.0)', width=2)\n",
    "    )\n",
    ")\n",
    "# Profit\n",
    "trace3 = go.Bar(\n",
    "    x=x_data, y=[0, 0, 0, 0, 0, 0, 370],\n",
    "    marker=dict(\n",
    "        color='rgba(50, 171, 96, 0.7)',\n",
    "        line=dict(color='rgba(50, 171, 96, 1.0)', width=2)\n",
    "    )\n",
    ")\n",
    "\n",
    "data = [trace0, trace1, trace2, trace3]\n",
    "layout = go.Layout(\n",
    "    title='Annual Profit- 2015',\n",
    "    barmode='stack',\n",
    "    paper_bgcolor='rgba(245, 246, 249, 1)',\n",
    "    plot_bgcolor='rgba(245, 246, 249, 1)',\n",
    "    showlegend=False\n",
    ")\n",
    "\n",
    "# 添加标注信息\n",
    "annotations = []\n",
    "for i in range(0, 7):\n",
    "    annotations.append(dict(\n",
    "        x=x_data[i], \n",
    "        y=y_data[i], \n",
    "        text=text[i],\n",
    "        font=dict(\n",
    "            family='Arial', \n",
    "            size=14, \n",
    "            color='rgba(245, 246, 249, 1)',\n",
    "        ), \n",
    "        showarrow=False)\n",
    "    )\n",
    "layout['annotations'] = annotations\n",
    "fig = go.Figure(data=data, layout=layout)\n",
    "plotly.offline.iplot(fig, filename='waterfall-bar-profit')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bar Chart with Relative Barmode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "name": "Trace0",
         "type": "bar",
         "uid": "7adfaec7-f30f-43b3-b2e3-57c1a244d82c",
         "x": [
          1,
          2,
          3,
          4
         ],
         "y": [
          1,
          4,
          9,
          16
         ]
        },
        {
         "name": "Trace1",
         "type": "bar",
         "uid": "64d22210-8031-4247-9fe5-e3f7019bd160",
         "x": [
          1,
          2,
          3,
          4
         ],
         "y": [
          6,
          -8,
          -4.5,
          8
         ]
        },
        {
         "name": "Trace2",
         "type": "bar",
         "uid": "b42267a5-dfd7-4124-a083-093484ea72e5",
         "x": [
          1,
          2,
          3,
          4
         ],
         "y": [
          -15,
          -3,
          4.5,
          -8
         ]
        },
        {
         "name": "Trace3",
         "type": "bar",
         "uid": "42bfaad9-8a86-438d-905d-457efd82e994",
         "x": [
          1,
          2,
          3,
          4
         ],
         "y": [
          -1,
          3,
          -3,
          -4
         ]
        }
       ],
       "layout": {
        "barmode": "relative",
        "title": "Relative Barmode",
        "xaxis": {
         "title": "X axis"
        },
        "yaxis": {
         "title": "Y axis"
        }
       }
      },
      "text/html": [
       "<div id=\"7a3c9285-2ad8-4a8e-8be2-e70683c2c09c\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"7a3c9285-2ad8-4a8e-8be2-e70683c2c09c\", [{\"name\": \"Trace0\", \"x\": [1, 2, 3, 4], \"y\": [1, 4, 9, 16], \"type\": \"bar\", \"uid\": \"ff0e0d19-7616-4108-9094-9d3a2bc026a0\"}, {\"name\": \"Trace1\", \"x\": [1, 2, 3, 4], \"y\": [6, -8, -4.5, 8], \"type\": \"bar\", \"uid\": \"3e9aeb28-7654-4a50-bfac-2320aad148e6\"}, {\"name\": \"Trace2\", \"x\": [1, 2, 3, 4], \"y\": [-15, -3, 4.5, -8], \"type\": \"bar\", \"uid\": \"51d2dcd5-05d0-42e2-926d-d91247ea59c9\"}, {\"name\": \"Trace3\", \"x\": [1, 2, 3, 4], \"y\": [-1, 3, -3, -4], \"type\": \"bar\", \"uid\": \"eee200ea-f529-4024-b3e8-00eac7a2d146\"}], {\"barmode\": \"relative\", \"title\": \"Relative Barmode\", \"xaxis\": {\"title\": \"X axis\"}, \"yaxis\": {\"title\": \"Y axis\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"7a3c9285-2ad8-4a8e-8be2-e70683c2c09c\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"7a3c9285-2ad8-4a8e-8be2-e70683c2c09c\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"7a3c9285-2ad8-4a8e-8be2-e70683c2c09c\", [{\"name\": \"Trace0\", \"x\": [1, 2, 3, 4], \"y\": [1, 4, 9, 16], \"type\": \"bar\", \"uid\": \"ff0e0d19-7616-4108-9094-9d3a2bc026a0\"}, {\"name\": \"Trace1\", \"x\": [1, 2, 3, 4], \"y\": [6, -8, -4.5, 8], \"type\": \"bar\", \"uid\": \"3e9aeb28-7654-4a50-bfac-2320aad148e6\"}, {\"name\": \"Trace2\", \"x\": [1, 2, 3, 4], \"y\": [-15, -3, 4.5, -8], \"type\": \"bar\", \"uid\": \"51d2dcd5-05d0-42e2-926d-d91247ea59c9\"}, {\"name\": \"Trace3\", \"x\": [1, 2, 3, 4], \"y\": [-1, 3, -3, -4], \"type\": \"bar\", \"uid\": \"eee200ea-f529-4024-b3e8-00eac7a2d146\"}], {\"barmode\": \"relative\", \"title\": \"Relative Barmode\", \"xaxis\": {\"title\": \"X axis\"}, \"yaxis\": {\"title\": \"Y axis\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"7a3c9285-2ad8-4a8e-8be2-e70683c2c09c\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = [1, 2, 3, 4]\n",
    "trace0 = {'x': x, 'y': [1, 4, 9, 16], 'name': 'Trace0', 'type': 'bar'}\n",
    "trace1 = {'x': x, 'y': [6, -8, -4.5, 8], 'name': 'Trace1', 'type': 'bar'}\n",
    "trace2 = {'x': x, 'y': [-15, -3, 4.5, -8], 'name': 'Trace2', 'type': 'bar'}\n",
    "trace3 = {'x': x, 'y': [-1, 3, -3, -4], 'name': 'Trace3', 'type': 'bar'}\n",
    "\n",
    "data = [trace0, trace1, trace2, trace3]\n",
    "layout = {\n",
    "    'xaxis': {'title': 'X axis'},\n",
    "    'yaxis': {'title': 'Y axis'},\n",
    "    # 这里的relative有点类似于stack，只不过区分正负值\n",
    "    'barmode': 'relative',\n",
    "    'title': 'Relative Barmode'\n",
    "}\n",
    "plotly.offline.iplot({'data': data, 'layout': layout}, filename='barmode-relative')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Population Pyramid Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "hoverinfo": "x",
         "marker": {
          "color": "powderblue"
         },
         "name": "Men",
         "orientation": "h",
         "type": "bar",
         "uid": "dda7cdd1-0b0b-4500-af8a-aa218e63c703",
         "x": [
          600,
          623,
          653,
          650,
          670,
          578,
          541,
          360,
          312,
          170
         ],
         "y": [
          0,
          10,
          20,
          30,
          40,
          50,
          60,
          70,
          80,
          90
         ]
        },
        {
         "hoverinfo": "text",
         "marker": {
          "color": "seagreen"
         },
         "name": "Women",
         "orientation": "h",
         "text": [
          500,
          523,
          573,
          650,
          670,
          578,
          541,
          411,
          322,
          230
         ],
         "type": "bar",
         "uid": "5f618bfd-4931-4d0c-998d-67d6f15ac966",
         "x": [
          -500,
          -523,
          -573,
          -650,
          -670,
          -578,
          -541,
          -411,
          -322,
          -230
         ],
         "y": [
          0,
          10,
          20,
          30,
          40,
          50,
          60,
          70,
          80,
          90
         ]
        }
       ],
       "layout": {
        "bargap": 0.1,
        "barmode": "overlay",
        "xaxis": {
         "range": [
          -1200,
          1200
         ],
         "ticktext": [
          1000,
          700,
          300,
          0,
          300,
          700,
          1000
         ],
         "tickvals": [
          -1000,
          -700,
          -300,
          0,
          300,
          700,
          1000
         ],
         "title": "Number"
        },
        "yaxis": {
         "title": "Age"
        }
       }
      },
      "text/html": [
       "<div id=\"a1b0387b-fb35-42bb-a426-a7cad4bb8f70\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"a1b0387b-fb35-42bb-a426-a7cad4bb8f70\", [{\"hoverinfo\": \"x\", \"marker\": {\"color\": \"powderblue\"}, \"name\": \"Men\", \"orientation\": \"h\", \"x\": [600, 623, 653, 650, 670, 578, 541, 360, 312, 170], \"y\": [0, 10, 20, 30, 40, 50, 60, 70, 80, 90], \"type\": \"bar\", \"uid\": \"357a6f63-c81d-43a1-a459-f3f1b1adb74e\"}, {\"hoverinfo\": \"text\", \"marker\": {\"color\": \"seagreen\"}, \"name\": \"Women\", \"orientation\": \"h\", \"text\": [500.0, 523.0, 573.0, 650.0, 670.0, 578.0, 541.0, 411.0, 322.0, 230.0], \"x\": [-500, -523, -573, -650, -670, -578, -541, -411, -322, -230], \"y\": [0, 10, 20, 30, 40, 50, 60, 70, 80, 90], \"type\": \"bar\", \"uid\": \"e4b63cf3-e95d-426a-a0a4-bd7471f2a3ce\"}], {\"bargap\": 0.1, \"barmode\": \"overlay\", \"xaxis\": {\"range\": [-1200, 1200], \"ticktext\": [1000, 700, 300, 0, 300, 700, 1000], \"tickvals\": [-1000, -700, -300, 0, 300, 700, 1000], \"title\": \"Number\"}, \"yaxis\": {\"title\": \"Age\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"a1b0387b-fb35-42bb-a426-a7cad4bb8f70\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"a1b0387b-fb35-42bb-a426-a7cad4bb8f70\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"a1b0387b-fb35-42bb-a426-a7cad4bb8f70\", [{\"hoverinfo\": \"x\", \"marker\": {\"color\": \"powderblue\"}, \"name\": \"Men\", \"orientation\": \"h\", \"x\": [600, 623, 653, 650, 670, 578, 541, 360, 312, 170], \"y\": [0, 10, 20, 30, 40, 50, 60, 70, 80, 90], \"type\": \"bar\", \"uid\": \"357a6f63-c81d-43a1-a459-f3f1b1adb74e\"}, {\"hoverinfo\": \"text\", \"marker\": {\"color\": \"seagreen\"}, \"name\": \"Women\", \"orientation\": \"h\", \"text\": [500.0, 523.0, 573.0, 650.0, 670.0, 578.0, 541.0, 411.0, 322.0, 230.0], \"x\": [-500, -523, -573, -650, -670, -578, -541, -411, -322, -230], \"y\": [0, 10, 20, 30, 40, 50, 60, 70, 80, 90], \"type\": \"bar\", \"uid\": \"e4b63cf3-e95d-426a-a0a4-bd7471f2a3ce\"}], {\"bargap\": 0.1, \"barmode\": \"overlay\", \"xaxis\": {\"range\": [-1200, 1200], \"ticktext\": [1000, 700, 300, 0, 300, 700, 1000], \"tickvals\": [-1000, -700, -300, 0, 300, 700, 1000], \"title\": \"Number\"}, \"yaxis\": {\"title\": \"Age\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"a1b0387b-fb35-42bb-a426-a7cad4bb8f70\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "women_bins = np.array([-500, -523, -573, -650, -670, -578, -541, -411, -322, -230])\n",
    "men_bins = np.array([600, 623, 653, 650, 670, 578, 541, 360, 312, 170])\n",
    "y = list(range(0, 100, 10))\n",
    "\n",
    "data = [\n",
    "    go.Bar(\n",
    "        y=y, x=men_bins, \n",
    "        orientation='h', \n",
    "        name='Men', \n",
    "        hoverinfo='x', \n",
    "        marker=dict(color='powderblue')\n",
    "    ),\n",
    "    go.Bar(\n",
    "        y=y, x=women_bins, \n",
    "        orientation='h', \n",
    "        name='Women', \n",
    "        text=-1 * women_bins.astype('int'),\n",
    "        hoverinfo='text', \n",
    "        marker=dict(color='seagreen')\n",
    "    )\n",
    "]\n",
    "\n",
    "layout = go.Layout(\n",
    "    yaxis=go.layout.YAxis(title='Age'),\n",
    "    xaxis=go.layout.XAxis(\n",
    "        range=[-1200, 1200],\n",
    "        tickvals=[-1000, -700, -300, 0, 300, 700, 1000],\n",
    "        ticktext=[1000, 700, 300, 0, 300, 700, 1000],\n",
    "        title='Number'\n",
    "    ),\n",
    "    barmode='overlay',\n",
    "    bargap=0.1\n",
    ")\n",
    "plotly.offline.iplot(dict(data=data, layout=layout), filename='bar_pyramid') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Error Bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "error_y": {
          "array": [
           1,
           2,
           1,
           1
          ],
          "arrayminus": [
           2,
           4,
           1,
           2
          ],
          "symmetric": false,
          "type": "data",
          "visible": true
         },
         "type": "scatter",
         "uid": "96bad971-e2ae-4303-9d32-92ef119688a5",
         "x": [
          0,
          1,
          2
         ],
         "y": [
          6,
          10,
          2
         ]
        }
       ],
       "layout": {}
      },
      "text/html": [
       "<div id=\"3244bfab-fbd5-4a91-9bdd-3e79bab24e2b\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"3244bfab-fbd5-4a91-9bdd-3e79bab24e2b\", [{\"error_y\": {\"array\": [1, 2, 1, 1], \"arrayminus\": [2, 4, 1, 2], \"symmetric\": false, \"type\": \"data\", \"visible\": true}, \"x\": [0, 1, 2], \"y\": [6, 10, 2], \"type\": \"scatter\", \"uid\": \"d523641a-014e-4599-ac64-1ee8020479e1\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"3244bfab-fbd5-4a91-9bdd-3e79bab24e2b\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"3244bfab-fbd5-4a91-9bdd-3e79bab24e2b\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"3244bfab-fbd5-4a91-9bdd-3e79bab24e2b\", [{\"error_y\": {\"array\": [1, 2, 1, 1], \"arrayminus\": [2, 4, 1, 2], \"symmetric\": false, \"type\": \"data\", \"visible\": true}, \"x\": [0, 1, 2], \"y\": [6, 10, 2], \"type\": \"scatter\", \"uid\": \"d523641a-014e-4599-ac64-1ee8020479e1\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"3244bfab-fbd5-4a91-9bdd-3e79bab24e2b\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = [\n",
    "    go.Scatter(\n",
    "        x=[0, 1, 2],\n",
    "        y=[6, 10, 2],\n",
    "        error_y=dict(\n",
    "            # 两种形式：1\n",
    "            # type='data',\n",
    "            # array=[1, 2, 3],\n",
    "            # 两种形式：2\n",
    "            type='data',\n",
    "            symmetric=False,\n",
    "            array=[1, 2, 1, 1],\n",
    "            arrayminus=[2, 4, 1, 2],\n",
    "            visible=True\n",
    "        )\n",
    "    )\n",
    "]\n",
    "plotly.offline.iplot(data, filename='basic-error-bar')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bar Chart with Error Bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "error_y": {
          "array": [
           1,
           0.5,
           1.5
          ],
          "type": "data",
          "visible": true
         },
         "name": "Control",
         "type": "bar",
         "uid": "be2a124e-7086-452a-8e3e-e4b7b79753ca",
         "x": [
          "Trial 1",
          "Trial 2",
          "Trial 3"
         ],
         "y": [
          3,
          6,
          4
         ]
        },
        {
         "error_y": {
          "array": [
           0.5,
           1,
           2
          ],
          "type": "data",
          "visible": true
         },
         "name": "Experimental",
         "type": "bar",
         "uid": "88832e46-e33c-4125-bc64-d827f2934ed4",
         "x": [
          "Trial 1",
          "Trial 2",
          "Trial 3"
         ],
         "y": [
          4,
          7,
          3
         ]
        }
       ],
       "layout": {
        "barmode": "group"
       }
      },
      "text/html": [
       "<div id=\"800891fd-c5e4-4b73-8240-d172f6d249f5\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"800891fd-c5e4-4b73-8240-d172f6d249f5\", [{\"error_y\": {\"array\": [1, 0.5, 1.5], \"type\": \"data\", \"visible\": true}, \"name\": \"Control\", \"x\": [\"Trial 1\", \"Trial 2\", \"Trial 3\"], \"y\": [3, 6, 4], \"type\": \"bar\", \"uid\": \"be2a124e-7086-452a-8e3e-e4b7b79753ca\"}, {\"error_y\": {\"array\": [0.5, 1, 2], \"type\": \"data\", \"visible\": true}, \"name\": \"Experimental\", \"x\": [\"Trial 1\", \"Trial 2\", \"Trial 3\"], \"y\": [4, 7, 3], \"type\": \"bar\", \"uid\": \"88832e46-e33c-4125-bc64-d827f2934ed4\"}], {\"barmode\": \"group\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"800891fd-c5e4-4b73-8240-d172f6d249f5\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"800891fd-c5e4-4b73-8240-d172f6d249f5\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"800891fd-c5e4-4b73-8240-d172f6d249f5\", [{\"error_y\": {\"array\": [1, 0.5, 1.5], \"type\": \"data\", \"visible\": true}, \"name\": \"Control\", \"x\": [\"Trial 1\", \"Trial 2\", \"Trial 3\"], \"y\": [3, 6, 4], \"type\": \"bar\", \"uid\": \"be2a124e-7086-452a-8e3e-e4b7b79753ca\"}, {\"error_y\": {\"array\": [0.5, 1, 2], \"type\": \"data\", \"visible\": true}, \"name\": \"Experimental\", \"x\": [\"Trial 1\", \"Trial 2\", \"Trial 3\"], \"y\": [4, 7, 3], \"type\": \"bar\", \"uid\": \"88832e46-e33c-4125-bc64-d827f2934ed4\"}], {\"barmode\": \"group\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"800891fd-c5e4-4b73-8240-d172f6d249f5\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace0 = go.Bar(\n",
    "    x=['Trial 1', 'Trial 2', 'Trial 3'],\n",
    "    y=[3, 6, 4],\n",
    "    name='Control',\n",
    "    error_y=dict(\n",
    "        type='data',\n",
    "        array=[1, 0.5, 1.5],\n",
    "        visible=True\n",
    "    )\n",
    ")\n",
    "trace1 = go.Bar(\n",
    "    x=['Trial 1', 'Trial 2', 'Trial 3'],\n",
    "    y=[4, 7, 3],\n",
    "    name='Experimental',\n",
    "    error_y=dict(\n",
    "        type='data',\n",
    "        array=[0.5, 1, 2],\n",
    "        visible=True\n",
    "    )\n",
    ")\n",
    "\n",
    "data = [trace0, trace1]\n",
    "layout = go.Layout(barmode='group')\n",
    "fig = go.Figure(data=data, layout=layout)\n",
    "plotly.offline.iplot(fig, filename='error-bar-bar')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
